import Vue from 'vue'
import VueRouter from 'vue-router'
import navbarRouter from "@/views/router";
import Main from "@/views/main";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        component: Main,
        redirect: '/home',
        children: [
            ...navbarRouter
        ]
    },
    {
        path: '/type',
        component: () => import('../views/type'),
        mete: {
            needLogin: true
        },
        name: 'type'
    },
    {
        path: '/details',
        component: () => import('../components/details/details'),
        name: 'details'
    },
    {
        path: '/login',
        component: () => import('../views/login'),
        name: 'login'
    },

]

// router.beforeEach((to, from, next) => {
//     if (to.meta.needLogin) {
//         if (localStorage.getItem('toke')) {
//             next()
//         } else {
//             next('/login')
//         }
//     }
// })

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    if (typeof (location) == "string") {
        var Separator = "&";
        if (location.indexOf('?') == -1) {
            Separator = '?';
        }
        location = location + Separator + "random=" + Math.random();
    }
    return routerPush.call(this, location).catch(error => error)
}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export default router
